/*
   @Copyright:LibreOJ
   @Author:   tjyemail
   @Problem:  https://loj.ac/problem/123
   @Language: C++
   @Datetime: 20-07-30 11:07
   */
// Optimize Prim with Mini-Heap, using forward star, time O(ElogV)

#include <bits/stdc++.h>
using namespace std;

#define VERTEX 200005
#define EDGE 500005
// linked forward star
struct Edge {
	int v, c, next;
	Edge(int v=0, int c=0, int next=-1) : v(v), c(c), next(next){}
	bool operator<(const Edge &e) const { return c>e.c; } // mini-heap
} edges[EDGE*2];
int heads[VERTEX];
int ie = 0;
inline void add(int u, int v, int c) {
	edges[ie] = Edge(v, c, heads[u]);
	heads[u] = ie++;
}

void prim(int s) {
	long long cost = 0, c = 0;
	priority_queue<Edge> vcosts; // mini-heap - vertex, cost
	for(vcosts.push({s, 0}); !vcosts.empty(); ){
		s=vcosts.top().v, c=vcosts.top().c;
		vcosts.pop();
		if (heads[s]==-1) continue; // already joined
		// add edges from s's adjacency vertex
		for(int k=heads[s]; k!=-1; k=edges[k].next) {
			if (heads[edges[k].v]!=-1)
				vcosts.push(edges[k]);
		}
		heads[s] = -1; // join vertex s
		cost += c;
	}
	// todo: check it is MST
	printf("%lld\n", cost);
}

int main()
{
	int v, e, a, b, c;
	scanf("%d %d", &v, &e); // v vertexs, e edges
	memset(heads, -1, sizeof(heads));
	for(; e--;) {
		scanf("%d %d %d", &a, &b, &c); // a->b, cost
		add(a, b, c);
		add(b, a, c); // undirect graph
	}
	prim(1); // start vertex from [1, v]
	return 0;
}
